<template>
  <div class="closing">
    <div>
      <jc-button
        :btn-arr="['reload', 'newy']"
        :disab-arr="['copy']"
        @refresh="handleGetUntreated"
        @newly="journali"
      />
    </div>
    <div class="header">
      <div class="header-name">
        <search :options="selectData" :msg="tacitly" @seek="collect" @hand="handleQuerySonClass" />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <filtration :ftype="ftype" :msg="tacitly" :select-data="selectData" :table-header="tableHeader1" @seachData="handleQuerySonClass" />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        @clickRow="listDetail"
      >
        <template v-slot:billSlot="col">
          <el-link type="primary" @click="listDetail(col.scope.row)">{{ col.scope.row.fnumber }}</el-link>
        </template>
        <template v-slot:tagSlot="col">
          <el-switch
            v-model="col.scope.row.isUseds"
            :active-text="$t('market.startUsing')"
            :inactive-text="$t('btn.disab')"
            @change="statusChange(col.scope.row)"
          />
        </template>
        <!--  操作  -->
        <template v-slot:btnSlot="col">
          <el-button size="mini" type="primary" @click="listDetail(col.scope.row)">{{ $t('market.detail') }}</el-button>
        </template>
      </jc-table>
    </div>
    <!--  分页器  -->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
    <!--  分录详情  -->
    <journalizing v-if="isJournaliVis" :title="title" :fgroup-id="fgroupId" @subject="handleGetUntreated" />
  </div>
</template>

<script>
import {
  querySubjectGroupList, changeSubjectgroupStatus
} from '@/api/financial/voucher'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'
import journalizing from '@/views/financial/voucherTemplate/components/journalizing'
export default {
  name: 'EntryType',
  components: {
    search,
    filtration,
    jcButton,
    journalizing
  },
  mixins: [searData],
  data() {
    return {
      ftype: 35,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      isJournaliVis: false, // 分录
      title: '',
      fgroupId: 0,
      // 表头
      tableData: [], // 销售数据
      tableHeight: '70vh',
      tableHeader: [
        { label: this.$t('market.journaliname'), prop: 'fname', width: '200' },
        { label: this.$t('market.journaliNum'), type: 'bill', prop: 'fnumber', width: '200' },
        { label: this.$t('market.journaliDesc'), prop: 'fdescription', width: '200' },
        { label: this.$t('market.isUsed'), type: 'tag', prop: 'isUsed', width: '200' },
        { label: this.$t('market.handle'), type: 'btn', width: '100' }
      ],
      tableHeader1: []
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '模板列表')
        } else {
          querySubjectGroupList({
            pageSize: 5000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '模板列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取生产列表数据
    async handleGetUntreated(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = { pageNum: this.currentPage, pageSize: this.size, search: this.searCollData.concat(this.details) }
      const RES = await querySubjectGroupList(DATA)
      if (RES.code === 0) {
        this.loading = false
        this.tableData = RES.data.list
        this.tableData.map(res => {
          res.isUsed === 1 ? res.isUseds = true : res.isUseds = false
        })
        this.isJournaliVis = false
        this.total = RES.data.total
      } else {
        this.loading = false
        this.$confirm(RES.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    //  新增
    journali() {
      this.fgroupId = 0
      this.isJournaliVis = true
      this.title = '新增分录'
    },
    // 详情
    listDetail(ev) {
      this.fgroupId = ev.fgroupId
      this.isJournaliVis = true
      this.title = '分录详情'
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 启用、禁用
    statusChange(ev) {
      const DATA = {
        fgroupId: ev.fgroupId,
        isUsed: ev.isUseds ? 1 : 0
      }
      changeSubjectgroupStatus(DATA).then(res => {
        if (res.code === 0) {
          this.handleGetUntreated()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },

    // 自定义列表
    customHandle(ev) {
      this.tableHeaders = []
      if (ev.header) {
        ev.header.forEach(item => {
          this.tableHeader1.forEach(res => {
            if (res.label === item) {
              this.tableHeaders.push(res)
            }
          })
        })
        this.tableHeader = this.tableHeaders
      } else if (ev.fname === '缺省方案') {
        this.tableHeader = this.oldTabHeader
      } else {
        this.tableHeader = this.tableHeader1
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.closing {
  @include listBom;

  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
